﻿using System.Globalization;
using System.Xml;
using System.Linq;
using System.Xml.Linq;

namespace WMM.Tapioca.Linkbase
{
    /// <summary>
    /// XBRL 2.1 Specification 5.2.3.2  
    /// The reference element enables XBRL taxonomies to ground the definitions of concepts in 
    /// authoritative statements in published business, financial and accounting literature.
    /// </summary>
    public class Reference : Resource
    {
        /// <summary>
        /// The text of this reference.
        /// </summary>
        public string ReferenceText
        {
            get;
            private set;
        }

        /// <summary>
        /// The culture of this reference.
        /// </summary>
        public CultureInfo Culture
        {
            get;
            private set;
        }

        //------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------
        internal Reference(XElement referenceElement, string label, string linkbaseSource, ExtendedLink extendedLink)
            : base(referenceElement, label, linkbaseSource, extendedLink)
        {
            this.Id = label;

            if (referenceElement.Elements().Count() > 0)
            {
                this.ReferenceText = referenceElement.Elements().ElementAt(0).Value;
            }

            XNamespace nsXLink = referenceElement.GetNamespaceOfPrefix("xlink");
            this.Role = (string)referenceElement.Attribute(nsXLink + "role") ?? string.Empty;

            XNamespace nsXml = referenceElement.GetNamespaceOfPrefix("xml");
            string languageValue = (string)referenceElement.Attribute(nsXml + "lang") ?? string.Empty;
            this.Culture = new CultureInfo(languageValue);
        }

    }
}
